
package vo;

import java.io.Serializable;
import java.util.ArrayList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * Serialization class for cal XML
 * @author jaje
 */
@XmlRootElement(name = "cal")
public class Cal implements Serializable {

	transient private static final long serialVersionUID = 1L;

	@XmlElementWrapper(name = "users")
	@XmlElement(name = "user")
	public ArrayList<User> users;

	@XmlElementWrapper(name = "tasks")
	@XmlElement(name = "task")
	public ArrayList<Task> tasks;

	public boolean containsTask(String taskId) {

		for (Task t : tasks) {

			if (t.id.equalsIgnoreCase(taskId)) return true; 

		}

		return false;
	}

	public Task getTask(String taskId) {

		for (Task t : tasks) {

			if (t.id.equalsIgnoreCase(taskId)) return t; 

		}

		return null;
	}

	public boolean addTask(Task newTask) {

		if (tasks.contains(newTask)) return false;
		tasks.add(newTask);
		System.out.println("task " +  newTask.id + " posted");
		return true;

	}

	public boolean deleteTask(String taskId ){

		if (!containsTask(taskId)) return false;
		tasks.remove(getTask(taskId));
		System.out.println("task " +  taskId + " deleted");
		return true;

	}

	public boolean updateTask(Task upTask) {

		if (!this.containsTask(upTask.id)) return false; 
		tasks.remove(getTask(upTask.id));
		tasks.add(upTask);
		System.out.println("task " +  upTask.id + " updated");
		return true;
	}

	public TaskList getUserTaskList(String userId) {

		TaskList tList = new TaskList();

		for (Task t : this.tasks) {
			if (t.impliesUser(userId)) tList.add(t);
		}

		return tList;
	}


	public boolean containsUser(String userId) {

		for (User u : users) {
			if (u.id.equals(userId) )
				return true; 
		}
		return false;	
	}

	public User getUser(String userId) {

		for (User u : users) {
			if (u.id.equals(userId) )
				return u; 
		}

		return null;
	}
}